<?php

/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/8
 * Time: 14:42
 */
namespace YMApi\Service;

/**
 * Class PromoteService
 * @package SDXApi\Service
 */
/**
 * Class PromoteService
 * @package SDXApi\Service
 */
/**
 * Class PromoteService
 * @package SDXApi\Service
 */
/**
 * Class PromoteService
 * @package SDXApi\Service
 */
class PromoteService
{
	/**
	 * @var \Model|\Think\Model
	 */
	protected $goodsModel;
	/**
	 * @var \Model|\Think\Model
	 */
	protected $orderModel;

	/**
	 * @var
	 */
	protected $errorMessage;

	/**
	 * @var
	 */
	protected $orderPromoteNumber;


	/**
	 * @var array
	 */
	protected $message = array();

	/**
	 * PromoteService constructor.
	 */
	public function __construct()
	{
		$this->goodsModel = D('Goods');
		$this->orderModel = D('Orderinfo');
	}

	/**
	 * 处理促销商品数据
	 * Created by Jerry
	 * @param $goodsInfo
	 * @return mixed
	 */
	public function formatPromoteData($goodsInfo)
	{
		$promote = $this->checkPromotePrice($goodsInfo);

		if ($promote !== false) {
			$goodsInfo['promote_message'] = $this->thenPromoteMessage($goodsInfo)->getNumberMessage();
			$goodsInfo['promote_price'] = $promote;
			$goodsInfo['promote_start_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_start_date']));
			$goodsInfo['promote_end_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_end_date']));
		}
		else
		{
			unset($goodsInfo['promote_end_date'], $goodsInfo['promote_start_date'], $goodsInfo['promote_goods_number'], $goodsInfo['promote_today_number'], $goodsInfo['promote_price']);
		}

		return $goodsInfo;
	}

	/**
	 * Created by Jerry
	 * @param $goodsInfo
	 * @return mixed
	 */
	public function formatPromoteCategoryData($goodsInfo)
	{
		$promote = $this->checkPromotePrice($goodsInfo);

		if ($promote !== false) {
			$goodsInfo['promote_message'] = $this->thenPromoteMessage($goodsInfo)->getNumberMessage();
			$goodsInfo['promote_price'] = $promote;
			$goodsInfo['promote_start_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_start_date']));
			$goodsInfo['promote_end_date'] = date('Y-m-d H:i:s', localtotime($goodsInfo['promote_end_date']));
		}
		else
		{
//			unset($goodsInfo['promote_end_date'], $goodsInfo['promote_start_date'], $goodsInfo['promote_goods_number'], $goodsInfo['promote_today_number'], $goodsInfo['promote_price']);
		}

		return $goodsInfo;
	}


	/**
	 * 获取促销价生效
	 * Created by Jerry
	 * @param $goods
	 * @return bool
	 */
	public function getPromotePriceStatus($goods)
	{
		if (empty($goods)) {
			return false;
		}

		$price = $this->checkPromotePrice($goods);

		if ($price > 0) {
			return true;
		}

		return false;
	}


	/**
	 * 检测促销价是否生效
	 * Created by Jerry
	 * @param $goodsInfo
	 * @return bool
	 */
	protected function checkPromotePrice($goodsInfo)
	{
		if (empty($goodsInfo) || $goodsInfo['is_buynow'] == 1) {
			return false;
		}

		$price = bargain_price($goodsInfo['promote_price'], $goodsInfo['promote_start_date'], $goodsInfo['promote_end_date']);

		if ($price > 0) {
			return $price;
		}

		return false;
	}

	/**
	 * 返回促销价是否生效
	 * Created by Jerry
	 * @param $uid
	 * @param $goods_id
	 * @param $goods_number
	 * @return bool
	 */
	public function isPromote($uid, $goods_id, $goods_number)
	{
		$goodsInfo = $this->getPromoteGoodsInfo($goods_id);

		return $this->checkPromote($uid, $goodsInfo, $goods_number);
	}


	/**
	 * Created by Jerry
	 * @param $goodsInfo
	 * @return PromoteService
	 */
	public function getPromoteGoodsNumber($goodsInfo)
	{
//		$goodsInfo = $this->getPromoteGoodsInfo($goods_id);
		return $this->thenPromoteMessage($goodsInfo);
	}

	/**
	 * 获取促销价格
	 * Created by Jerry
	 * @param $uid
	 * @param $goods_id
	 * @return float
	 */
	public function getPromotePrice($uid, $goods_id)
	{

		$goodsInfo = $this->getPromoteGoodsInfo($goods_id);

		return $this->checkPromotePrice($goodsInfo);

	}

	/**
	 * Created by Jerry
	 * @return mixed
	 */
	public function getErrorMessage()
	{
		return $this->errorMessage;
	}

	/**
	 * Created by Jerry
	 * @return mixed
	 */
	public function getNumberMessage()
	{
		$message = implode(',', $this->message);
		return trim($message, ',');
	}

	/**
	 * 处理商品价格
	 * Created by Jerry
	 * @param $goodsInfo
	 * @return float
	 */
//	protected function checkPromotePrice($goodsInfo)
//	{
//		$promotePrice = bargain_price($goodsInfo['promote_price'], $goodsInfo['promote_start_date'], $goodsInfo['promote_end_date']);
//
////		if (empty($promotePrice)) {
////			$price = $this->goodsModel->get_final_price($goodsId, 1, false, array(), $uid);
////		}else{
////			$price = $promotePrice;
////		}
//
//
//		return $promotePrice;
//	}


	/**
	 * 获取促销商品信息
	 * Created by Jerry
	 * @param $goods_id
	 * @return mixed
	 */
	protected function getPromoteGoodsInfo($goods_id)
	{
		return $this->goodsModel->get_goods_info_by_promote($goods_id);
	}

	/**
	 * 处理促销价
	 * Created by Jerry
	 * @param $uid
	 * @param $goodsInfo
	 * @param $goodsNumber
	 * @return bool
	 */
	protected function checkPromote($uid, $goodsInfo, $goodsNumber)
	{

		if (empty($uid) && empty($goodsInfo) && empty($goodsNumber)) {
			return false;
		}

		$count = $this->findOrderPromoteByToDay($uid, $goodsInfo['goods_sn']);
		$orderPromoteNumber = $this->findOrderPromoteByNumberTime($uid, $goodsInfo);

		return $this->thenPromote($goodsInfo, $count, $goodsNumber, $orderPromoteNumber);
	}

	/**
	 * 返回当天此会员促销价的单个商品订单总数
	 * Created by Jerry
	 * @param $user_id
	 * @param $goods_sn
	 * @return mixed
	 */
	protected function findOrderPromoteByToDay($user_id, $goods_sn)
	{
		return $this->orderModel->get_order_promote_by_to_day_for_count($user_id, $goods_sn);
	}

	/**
	 * 返回促销价期间内该用户此商品所有订单总数
	 * Created by Jerry
	 * @param $uid
	 * @param $goodsInfo
	 * @return mixed
	 */
	protected function findOrderPromoteByNumberTime($uid, $goodsInfo)
	{
		return $this->orderModel->get_order_promote_by_number($uid, $goodsInfo['goods_sn'], $goodsInfo['promote_start_date'], $goodsInfo['promote_end_date']);
	}


	/**
	 * 判断是否在促销价限制范围内
	 * Created by Jerry
	 * @param $goodsInfo
	 * @param $toDayNumber
	 * @param $goodsNumber
	 * @param $orderPromoteNumber
	 * @return bool
	 */
	protected function thenPromote($goodsInfo, $toDayNumber, $goodsNumber, $orderPromoteNumber)
	{
		$time = gmtime();

		if (($goodsInfo['promote_start_date'] > 0 && $goodsInfo['promote_end_date'] > 0)) {
			if (($goodsInfo['promote_start_date'] < $time &&  $goodsInfo['promote_end_date'] < $time)) {
//			促销商品已过期
				$this->errorMessage = '促销商品已过期，请重试';
				return true;
			}

			if ($goodsInfo['promote_start_date'] > $time &&  $goodsInfo['promote_end_date'] > $time) {
//			促销商品未开始
				$this->errorMessage = '促销商品未开始，请重试';
				return true;
			}
		}

//		限购商品数量
		if ($goodsInfo['promote_goods_number'] > 0 && $goodsInfo['promote_goods_number'] < $goodsNumber) {
			$this->errorMessage = '此促销商品已超出单次购买数量超过上限';
			return false;
		}

//		每天限购数量
		if ($goodsInfo['promote_today_number'] > 0 && $goodsInfo['promote_today_number'] <= $toDayNumber) {
			$this->errorMessage = '此促销商品已超出当天可购买数量';
			return false;
		}

		if ($goodsInfo['promote_today_number'] < 0 && abs($goodsInfo['promote_today_number']) <= $orderPromoteNumber) {
			$this->errorMessage = '此促销商品已超出此次促销活动可购买数量';
			return false;
		}

//		促销商品数量
//		if ($goodsInfo['promote_number'] < $goodsNumber) {
//			return false;
//		}
		return true;
	}


	/**
	 * Created by Jerry
	 * @param $goodsInfo
	 * @return $this
	 */
	protected function thenPromoteMessage($goodsInfo)
	{
		$time = gmtime();
		if ($goodsInfo['promote_start_date'] < $time &&  $goodsInfo['promote_end_date'] < $time) {
//			促销商品已过期
			array_push($this->message, '');
			return $this;
		}

		if ($goodsInfo['promote_start_date'] > $time &&  $goodsInfo['promote_end_date'] > $time) {
//			促销商品未开始
			array_push($this->message, '');
			return $this;
		}

		if ($goodsInfo['promote_goods_number'] > 0) {
			array_push($this->message, '每单限购' . $goodsInfo['promote_goods_number'] . '份');
		}

		if ($goodsInfo['promote_today_number'] > 0) {
			array_push($this->message, '每天限购' . $goodsInfo['promote_today_number'] . '单');
		}

		if ($goodsInfo['promote_today_number'] < 0) {
			array_push($this->message, '每人限购' .abs($goodsInfo['promote_today_number']). '单');
		}

		return $this;
	}

}